package com.cms.content.article.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.framework.utils.BeanUtilEx;
import com.cms.framework.utils.page.PageView;
import com.cms.content.article.dao.ArticleDAO;
import com.cms.content.article.pojo.Article;
import com.cms.content.article.pojo.ArticleWithBLOBs;
import com.cms.content.article.pojo.dto.ArticleWithBLOBsDTO;
import com.cms.content.article.service.ArticleService;
@Service
public class ArticleServiceImpl implements ArticleService {
	@Autowired
	ArticleDAO articleDAO;

	@Override
	public boolean saveArticle(ArticleWithBLOBs article) {
		if(article.getId()==null){
			return articleDAO.insert(article)>0;
		}else{
			return articleDAO.updateByPrimaryKey(article)>0;
		}
	}

	@Override
	public boolean deleteById(Integer id) {
		return articleDAO.deleteByPrimaryKey(id)>0;
	}

	@Override
	public ArticleWithBLOBs findById(Integer id) {
		return articleDAO.selectByPrimaryKey(id);
	}

	@Override
	public List<ArticleWithBLOBs> findList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageView<ArticleWithBLOBsDTO> findListByPage(
			PageView<ArticleWithBLOBsDTO> pageView,
			ArticleWithBLOBsDTO articleWithBLOBsParams) {
		List<ArticleWithBLOBs> list=articleDAO.selectByPage(pageView, articleWithBLOBsParams);
		List<ArticleWithBLOBsDTO> dtoList=new ArrayList<ArticleWithBLOBsDTO>();
		for(ArticleWithBLOBs articleWithBLOBs:list){
			ArticleWithBLOBsDTO articleWithBLOBsDTO=new ArticleWithBLOBsDTO();
			try {
				BeanUtilEx.copyProperties(articleWithBLOBsDTO, articleWithBLOBs);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			dtoList.add(articleWithBLOBsDTO);
		}
		pageView.setRecords(dtoList);
		return pageView;
	}
	
}
